package com.springboot.rookie.controller;

import com.springboot.rookie.controller.system.SysUserController;
import com.springboot.rookie.pojo.SysUserBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

@Controller
public class IndexController {

    @Autowired
    SysUserController sysUserController;

    @PostMapping("/login")
    public String login(@RequestParam("username") String username,
                        @RequestParam("password") String password,
                        Model model,
                        HttpSession session) {

        SysUserBean userBean = sysUserController.getUserByName(username);

        if (userBean == null || !userBean.getPassword().equals(password)) {
            model.addAttribute("message", "用户名或密码错误！");
            return "index";
        }

        session.setAttribute("username", username);
        return "redirect:/main"; //重定向
    }

}
